from django.shortcuts import render

# Create your views here.
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from areas.models import Area
from areas.serializers import AreaSerializer, SubAreaSerializer

'''
请求方式 GET /areas/infos/

请求省份数据


'''

#
# class AreasInfosAPIview(APIView):
#
#     def get(self,request):
#         area = Area.objects.filter(parent_id = None)
#
#         serializer = AreasInfosAPIviewSerialzier(data=area)
#         serializer.is_valid(raise_exception=True)
#
#         return Response(serializer.data)

# cacheresponseMixin 是继承　cache_response这个装饰器　实现的这个方法　并在配置中设置时间


class AreasViewSet(CacheResponseMixin,ReadOnlyModelViewSet):

    pagination_class = None  # 区划信息不分页

    def get_queryset(self):
        """
        提供数据集
        """
        if self.action == 'list':
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()

    def get_serializer_class(self):
        """
        提供序列化器
        """
        if self.action == 'list':
            return AreaSerializer
        else:
            return SubAreaSerializer